package lxs.swift.instance.action;

import lxs.swift.instance.StructInstance;

import java.util.function.Function;

public class DoubleAction<Key> extends Action<Key, Double> {

    public DoubleAction(StructInstance<Key> instance, Function<Object, Double> convertor) {
        super(instance, convertor);
    }

    public void acc(Key key, double num) {
        Double value = getOr(key, 0.0);
        set(key, value + num);
    }

    public void mul(Key key, double num) {
        Double value = getOr(key, 0.0);
        set(key, value * num);
    }

    public void div(Key key, double num) {
        Double value = getOr(key, 0.0);
        set(key, value / num);
    }

    public void mod(Key key, double num) {
        Double value = getOr(key, 0.0);
        set(key, value % num);
    }

    @SafeVarargs
    public final double sum(Key... keys) {
        double sum = 0;
        for (Key key : keys) {
            Double v = getOr(key, 0.0);
            sum += v;
        }
        return sum;
    }

    public long round(Key key) {
        return Math.round(getOr(key, 0.0));
    }

    public double ceil(Key key) {
        return Math.ceil(getOr(key, 0.0));
    }

    public double floor(Key key) {
        return Math.floor(getOr(key, 0.0));
    }
}
